<?php
App::uses('AppController', 'Controller');
/**
 * Reports Controller
 *
 * @property Report $Report
 */
class SchedulesController extends AppController {
	//var $uses = array('Report');
	
	public function index() 
	{
		$uid = CakeSession::read("Auth.User.UserId");
		$utype = CakeSession::read("Auth.User.UserType");
		if($utype == 'Administrator')
		{
			$result = $this->Schedule->allJob();
		}
		else
		{
			$result = $this->Schedule->jobList($uid);
		}
		$this->set('jobs',$result);
	}
	//GOOGLE CHARTS Display Data
	//Export to CSV
	
	public function view($jobId = null, $requestId = null, $requestScheduleId = null)
	{
		$uid = CakeSession::read("Auth.User.UserId");
		$uName = CakeSession::read("Auth.User.UserName");
		$utype = CakeSession::read("Auth.User.UserType");
		//if($utype == 'Administrator')
		$userCheck = $this->Schedule->verifyUserJob($uid, $jobId, $requestId, $requestScheduleId);
		if($utype == 'Administrator' || $userCheck)
		{
			$job = $this->Schedule->getJobRequest($jobId, $requestId);
			$this->set('jobs',$job);
			if($utype == 'Administrator')
			{
				$staff = $this->Schedule->getRouteStaffAdmin($requestId);
				$this->set('staffs',$staff);
				foreach($staff as $worker)
				{
					$contactid = $worker[0]['UserId'];
					$contactname = preg_replace('/[^\p{L}\p{N}\s]/u', '', $worker[0]['FirstName']);
					//echo $contactid;
					$worker = $this->Schedule->getRouteContact($requestId, $contactid);
					
					$this->set($contactname, $worker);
				}
			}
			else
			{
				$staff = $this->Schedule->getRouteStaff($requestId, $uid);
				$this->set('staffs',$staff);
				foreach($staff as $worker)
				{
					$contactid = $worker[0]['UserId'];
					$contactname = preg_replace('/[^\p{L}\p{N}\s]/u', '', $worker[0]['FirstName']);
					//echo $contactid;
					$worker = $this->Schedule->getRouteContact($requestId, $contactid);
					
					$this->set($contactname, $worker);
				}
				
			}
			
			$clients = $this->Schedule->getClients($requestId);
			$this->set('clients',$clients);
			$notes = $this->Schedule->getNotes($requestId);
			$this->set('notes',$notes);			
		}
		
		else
		{
			$this->Session->setFlash(__('You are not Permitted to view this Job'));
			$this->redirect(array('action' => 'index'));
		}
	}

}

